package com.ternnetwork.oa.service.impl.work;

import com.ternnetwork.commons.utils.EmailUtils;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.commons.utils.ValidateUtils;
import com.ternnetwork.oa.dto.work.Email;
import com.ternnetwork.oa.service.work.EventEmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

@Component
@Service
@Slf4j
public class EventEmailServiceImpl implements EventEmailService {


    @Value("${app.oa.email.userName}")
    private String emailUserName;

    @Value("${app.oa.email.password}")
    private String emailPassword;

    @Value("${app.oa.email.tpl.event-create}")
    private String emailTplEventCreate;

    @Value("${app.oa.email.tpl.event-update}")
    private String emailTplEventUpdate;

    @Value("${app.oa.email.tpl.event-delete}")
    private String emailTplEventDelete;

    @Async("taskExecutor")
    public  void sendEmailForCreate(String title,String start,String end,String userName, String emailAddress) {

        try {

            if(ValidateUtils.isEmail(emailAddress)){

                Email email = getEmail(emailTplEventCreate);

                String text=email.getText().replaceAll("\\(title\\)",title)
                        .replaceAll("\\(username\\)",userName)
                        .replaceAll("\\(start\\)",start)
                        .replaceAll("\\(end\\)",end);
                EmailUtils.sendTextEmailWithTSL(emailUserName,emailPassword,emailAddress,"smtp.partner.outlook.cn","true","587",email.getSubject(),text,null);

            }

        } catch (Exception e) {
            log.info("",e);
        }
    }

    @Async("taskExecutor")
    public  void sendEmailForUpdate(String title,String userName, String emailAddress) {
        try {

            if(ValidateUtils.isEmail(emailAddress)){
                Email email = getEmail(emailTplEventUpdate);
                String text=email.getText().replaceAll("\\(title\\)",title)
                        .replaceAll("\\(username\\)",userName);
                EmailUtils.sendTextEmailWithTSL(emailUserName,emailPassword,emailAddress,"smtp.partner.outlook.cn","true","587",email.getSubject(),text,null);
            }

        } catch (Exception e) {
            log.info("",e);
        }
    }

    @Async("taskExecutor")
    public  void sendEmailForDelete(String title,String userName, String emailAddress) {

        try {

            if(ValidateUtils.isEmail(emailAddress)){
                Email email = getEmail(emailTplEventDelete);
                String text=email.getText().replaceAll("\\(title\\)",title)
                        .replaceAll("\\(username\\)",userName);
                EmailUtils.sendTextEmailWithTSL(emailUserName,emailPassword,emailAddress,"smtp.partner.outlook.cn","true","587",email.getSubject(),text,null);
            }

        } catch (Exception e) {
            log.info("",e);
        }
    }

    private Email getEmail(String tpl) throws IOException {

        String json= FileUtils.readFileToString(new File(tpl),"UTF-8");

        return JsonUtils.jsonToObjectThrowsException(json, Email.class);
    }

}
